
package cn.bs.socket.server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class DatagramSocketServer {
      static int port = 7200;
    private byte[] buf = new byte[1000];
    private DatagramPacket dp = new DatagramPacket(buf,buf.length);
    private DatagramSocket socket;


     public DatagramSocketServer() throws IOException {
         System.out.println("start....server.................");

         socket = new DatagramSocket(port, InetAddress.getLocalHost());
         while(true){
             socket.receive(dp);
             System.out.println(new String(buf,0,dp.getLength()));
             String rcdp = Dgram.toString(dp) + ",from address:" + dp.getAddress() + ",port" + dp.getPort();
             System.out.println(rcdp);
             String str = "server:" + rcdp;

             DatagramPacket echodp = Dgram.toDataGram(str, dp.getAddress(), dp.getPort());
             socket.send(echodp);
           //  socket.close();
         }



     }
}
